package com.wtwd.device.util;

import java.net.*;
import java.util.Enumeration;

/**
 * @author zdl
 * @date 2022/1/14 11:28
 */
public class StringUtils {

    public static long ipToLong(String strIp) {
        String[] ip = strIp.split("\\.");
        return (Long.parseLong(ip[0]) << 24) + (Long.parseLong(ip[1]) << 16) + (Long.parseLong(ip[2]) << 8)
                + Long.parseLong(ip[3]);
    }



    public static String getLocalIp() {
        try {
            for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements(); ) {
                NetworkInterface item = e.nextElement();
                for (InterfaceAddress address : item.getInterfaceAddresses()) {
                    if (item.isLoopback() || !item.isUp()) {
                        continue;
                    }
                    if (address.getAddress() instanceof Inet4Address) {
                        Inet4Address inet4Address = (Inet4Address) address.getAddress();
                        return inet4Address.getHostAddress();
                    }
                }
            }
            return InetAddress.getLocalHost().getHostAddress();
        } catch (SocketException | UnknownHostException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        String localIp = "192.168.15.195";
        System.out.println(StringUtils.ipToLong(localIp));
        String localIp2 = "192.168.15.173";
        System.out.println(StringUtils.ipToLong(localIp2));
    }

    public static  boolean isEmpty(String value) {
        return (value == null || value.trim().length() == 0);
    }

    public static  boolean isNotEmpty(String value) {
        return !isEmpty(value);
    }

}
